package com.lnb.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lnb.entity.Roles;
import com.lnb.entity.UserRoles;
import com.lnb.service.IUserRolesService;
import com.lnb.util.Result;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liningbo
 * @since 2020-05-21
 */
@RestController
@RequestMapping("/user-roles")
public class UserRolesController {

	@Autowired
	IUserRolesService service;
	
	/**
	 * 通过用户id获取指定用户的角色列表
	 * @param uid
	 * @return
	 */
	@GetMapping("/getRolesByUserId")
	public Object getRolesByUserId(@RequestParam Integer uid) {
		List<Roles> list=service.getRolesByUserId(uid)  ;
		return list;
	}
	
	/**
	 * 给指定的用户批量添加角色
	 * @param userId  用户id， 举例：1001
	 * @param roleIds 角色id组成的字符串，举例：1,2,3
	 * @return
	 */
	@PostMapping("/addRolesByUserId")
	public Object addRolesByUserId(@RequestParam Integer userId,@RequestParam String roleIds) {
		List<UserRoles> list = new ArrayList<UserRoles>();
		
		String[] arr = roleIds.split(",");
		for(String s:arr) {
			list.add(new UserRoles(0,userId,Integer.valueOf(s)));
		}
			
		boolean res = service.saveBatch(list);
		if(res) {
			return new Result(0,"批量添加成功");
		}else {
			return new Result(1,"批量添加失败");
		}
	}
	
	
	
	
	
	
	/**
	 * 给指定的用户批量移除角色
	 * @param userId  用户id， 举例：1001
	 * @param roleIds 角色id组成的字符串，举例：1,2,3
	 * @return
	 */
	@PostMapping("/removeRolesByUserId")
	public Object removeRolesByUserId(@RequestParam Integer userId,@RequestParam String roleIds) {
		boolean res = service.removeRolesByUserId(userId,roleIds);
		if(res) {
			return new Result(0,"批量移除成功");
		}else {
			return new Result(1,"批量移除失败");
		}
	}

}
